package com.llg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llg.common.R;
import com.llg.dto.DishDto;
import com.llg.entity.Category;
import com.llg.entity.Dish;
import com.llg.entity.DishFlavor;
import com.llg.service.CategoryService;
import com.llg.service.DishFlavorService;
import com.llg.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){

        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页对象
        Page<Dish> dishInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoInfo = new Page<>();
        //构造条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        //根据name进行模糊查询
        queryWrapper.like(name != null,Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(dishInfo,queryWrapper);

        //records里面记录了整个源数据
        BeanUtils.copyProperties(dishInfo,dishDtoInfo,"records");

        //获取源数据
        List<Dish> records = dishInfo.getRecords();

        List<DishDto> newRecords = new ArrayList<>();

        for (Dish record : records) {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(record,dishDto);

            Long categoryId = record.getCategoryId();//获取分类id
            //根据分类Id查询分类
            Category category = categoryService.getById(categoryId);

            if(category != null) {
                dishDto.setCategoryName(category.getName());
            }
            newRecords.add(dishDto);
        }

        dishDtoInfo.setRecords(newRecords);

        return R.success(dishDtoInfo);
    }

    /**
     * 根据id回显菜品信息以及口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){

        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);

        return R.success("修改成功");
    }

    /**
     * 根据分类id查询对应的菜品
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> getByCategoryId(Dish dish){
        List<DishDto> res = new ArrayList<>();

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);
        for (Dish tDish : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(tDish,dishDto);
            //查询菜品对应的口味数据
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,tDish.getId());
            List<DishFlavor> list1 = dishFlavorService.list(queryWrapper1);
            //设置口味数据
            dishDto.setFlavors(list1);
            res.add(dishDto);
        }

        return R.success(res);
    }

    /**
     * 删除and批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> remove(@RequestParam List<Long> ids){
        //删除菜品表数据
        dishService.removeByIds(ids);

        //删除口味表数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper);

        return R.success("删除成功");
    }

    @PostMapping("/status/{state}")
    public R<String> updateStatus(@PathVariable int state,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        List<Dish> list = dishService.list(queryWrapper);
        for (Dish dish : list) {
            dish.setStatus(state);
        }
        dishService.updateBatchById(list);
        return R.success("修改成功");
    }
}
